# -*- coding: utf-8 -*-
"""
Created on Tue Aug 14 09:10:58 2018

@author: jiujiujiu
"""

import requests
from bs4 import BeautifulSoup
import re
from cs import day
from WriteData import writedata
from Tk import genearteMD5


# 获取页面
def getHTMLText(url):
    header = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36'}
    try:
        r = requests.get(url, timeout=30, headers=header)
        r.raise_for_status()  # 如果状态不是200, 引发HTTPError异常
        r.encoding = r.apparent_encoding
        html = r.text
        return html

    except:
        return None


# 提取url
def get_url(u):
    urllist = []
    html = getHTMLText(u)
    if not html:
        return urllist
    soup = BeautifulSoup(html, 'lxml')
    urls = soup.select('.content-item .news-item')
    for u in urls:
        # 访问urli
        url = 'http://www.bihucj.com/' + u.get('href')
        urllist.append(url)
    return urllist


# 提取url页面中时间字段
def get_date(u):
    datelist = []
    urls = get_url(u)
    if not urls:
        return datelist
    for url in urls:
        html = getHTMLText(url)
        regex = re.compile("<span>(\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2}</span>")
        date = re.findall(regex, html)
        if date:
            datelist.append(date[0])
    return datelist


def count_process_one():  # 资讯-最新
    d1 = 0
    d2 = 0
    d3 = 0
    d4 = 0
    a = 1
    while 1:
        s = d1 + d2 + d3
        u = 'http://www.bihucj.com/media.html?&page={}'
        u = u.format(a)
        datelist = get_date(u)
        print('----币虎财经新闻第{}页获取中----'.format(a))
        for date in datelist:
            s = day(date)
            if s == 0:
                d1 += 1
            elif s == 1:
                d2 += 1
            elif s == 2:
                d3 += 1
            else:
                d4 += 1
        a += 1
        if d4 > 0:
            break
    return (d1, d2, d3)


def main():
    u = '币虎财经'
    t1 = count_process_one()
    tk = genearteMD5(u)
    D = {tk: {"name": u, "today": t1[0], "yesterday": t1[1], "frontday": t1[2]}}
    writedata(D)
    return D


if __name__ == '__main__':
    print(main())
